<?php

namespace app\admin\controller\system;

use app\admin\controller\adminAuthController;
use app\core\util\ToolUtil;
use app\base\model\system\SystemAdmin as AdminModel;
use app\core\exception\ErrorCode;

class SystemAdminController extends adminAuthController
{

    protected $needRecordAdminOperationLogAction = ['createAdmin', 'updateAdmin', 'deleteAdmin', 'updateAdminPwd', 'setDefaultPwd'];

    /**
     * 获取管理员列表
     *
     * @return \think\Response
     */
    public function getAdminList()
    {
        $admin = $this->adminInfo;
        $where = ToolUtil::getMore([['admin_name', ''], ['real_name', ''], ['role_ids', '']], $this->request);
        $where['level'] = $admin->level;
        $list = AdminModel::getAdminList($where);
        return $this->successReturn($list);
    }

    /**
     * Desc: 创建新的管理员
     * Date: 2019/10/21 22:59
     * Author: yiyu
     * @return \think\response\Json
     */
    public function createAdmin()
    {
        $data = ToolUtil::postMore([
            'admin_name',
            'real_name',
            ['head_photo', ''],
            ['role_ids', []],
        ], $this->request);

        if (!$data['admin_name']) {
            return $this->errorReturn(ErrorCode::COMMON_LACK_PARAMS, '请输入账号');
        }

        if (!$data['real_name']) {
            return $this->errorReturn(ErrorCode::COMMON_LACK_PARAMS, '请填入真实姓名');
        }

        if (!$data['role_ids']) {
            return $this->errorReturn(ErrorCode::COMMON_LACK_PARAMS, '请选择至少一个角色');
        }

        if (AdminModel::be($data['admin_name'], 'admin_name')) {
            return $this->errorReturn(ErrorCode::COMMON_LACK_PARAMS, '账号已存在');
        }

        $salt_str = ToolUtil::generateStr(6);

        $gen_pwd = ToolUtil::generateStr();

        $data['admin_pwd'] = ToolUtil::getEncryptPwd($gen_pwd, $salt_str);
        $data['pwd_salt'] = $salt_str;
        $data['level'] = $this->adminInfo['level'] + 1;
        $res = AdminModel::set($data);

        return $res ? $this->successReturn(['gen_pwd' => $gen_pwd]) : $this->errorReturn();
    }

    /**
     * Desc: 修改指定的管理员信息
     * Date: 2019/10/21 23:00
     * Author: yiyu
     * @return \think\response\Json
     */
    public function updateAdmin()
    {

        $admin_id = input('admin_id');

        if (ToolUtil::notIsValidId($admin_id)) {
            return $this->errorReturn(ErrorCode::COMMON_INVALID_PARAMS);
        }

        $data = ToolUtil::postMore([
            'admin_name',
            'admin_pwd',
            'new_pwd',
            'real_name',
            ['head_photo', ''],
            ['role_ids', []],
        ], $this->request);

        if (!$data['admin_name']) {
            return $this->errorReturn(ErrorCode::COMMON_LACK_PARAMS, '请输入账号');
        }

        if (!$data['real_name']) {
            return $this->errorReturn(ErrorCode::COMMON_LACK_PARAMS, '请填入真实姓名');
        }

        if (!$data['role_ids']) {
            return $this->errorReturn(ErrorCode::COMMON_LACK_PARAMS, '请选择至少一个角色');
        }

        $admin_info = AdminModel::get($admin_id);

        if (empty($admin_info)) {
            return $this->errorReturn(ErrorCode::DATA_NOT_FOUND);
        }

        if ($admin_info['admin_name'] != $data['admin_name']) {
            return $this->errorReturn(ErrorCode::COMMON_LACK_PARAMS, '用户名不能修改');
        }

        $res = AdminModel::edit($data, $admin_id);

        return $res ? $this->successReturn() : $this->errorReturn();
    }

    /**
     * Desc: 删除指定的管理员
     * Date: 2019/10/21 22:45
     * Author: yiyu
     * @return \think\response\Json
     */
    public function deleteAdmin()
    {

        $admin_id = input('admin_id');

        if (ToolUtil::notIsValidId($admin_id)) {
            return $this->errorReturn(ErrorCode::COMMON_INVALID_PARAMS);
        }

        $admin_info = AdminModel::get($admin_id);

        if (empty($admin_info)) {
            return $this->errorReturn(ErrorCode::DATA_NOT_FOUND);
        }

        $res = AdminModel::edit(['is_del' => 1, 'status' => 0], $admin_id, 'admin_id');

        return $res ? $this->successReturn() : $this->errorReturn();
    }

    /**
     * Desc: 获取我的信息
     * Date: 2019/10/21 23:01
     * Author: yiyu
     * @return \think\response\Json
     */
    public function getMyInfo()
    {
        $data['admin_info'] = $this->adminInfo;//获取当前登录的管理员
        unset($data['admin_info']['admin_pwd'], $data['admin_info']['pwd_salt']);
        return $this->successReturn($data);
    }

    /**
     * Desc: 修改密码
     * Date: 2019/10/21 21:58
     * Author: yiyu
     * @return \think\response\Json
     */
    public function updateAdminPwd()
    {
        $data = ToolUtil::postMore([
            ['new_pwd_one', ''],
            ['new_pwd_two', ''],
            ['admin_pwd', ''],
        ], $this->request);

        if (empty($data['admin_pwd'])) {
            $this->errorReturn(ErrorCode::COMMON_LACK_PARAMS, '请输入原始密码');
        }

        if (empty($data['new_pwd_one'])) {
            $this->errorReturn(ErrorCode::COMMON_LACK_PARAMS, '请输入新密码');
        }

        if (empty($data['new_pwd_two'])) {
            $this->errorReturn(ErrorCode::COMMON_LACK_PARAMS, '请输入确认新密码');
        }

        if ($data['new_pwd_one'] != $data['new_pwd_two']) {
            $this->errorReturn(ErrorCode::COMMON_LACK_PARAMS, '两次密码不一样');
        }

        $adminInfo = $this->adminInfo;//获取当前登录的管理员

        if ($adminInfo['admin_pwd'] != ToolUtil::getEncryptPwd($data['admin_pwd'], $adminInfo['pwd_salt'])) {
            $this->errorReturn(ErrorCode::COMMON_LACK_PARAMS, '原始密码错误');
        }

        $salt_str = ToolUtil::generateStr(6);

        $data['pwd_salt'] = $salt_str;
        $data['admin_pwd'] = ToolUtil::getEncryptPwd($data['admin_pwd'], $salt_str);
        unset($data['new_pwd_one'], $data['new_pwd_two']);
        $res = AdminModel::edit($data, $adminInfo['admin_id']);

        return $res ? $this->successReturn(null, '修改成功,请重新登录') : $this->errorReturn();

    }

    /**
     * Desc: 重置密码
     * Date: 2019/10/21 23:01
     * Author: yiyu
     * @return \think\response\Json
     */
    public function setDefaultPwd()
    {

        $salt_str = ToolUtil::generateStr(6);

        $gen_pwd = ToolUtil::generateStr();

        $data['admin_pwd'] = ToolUtil::getEncryptPwd($gen_pwd, $salt_str);
        $data['pwd_salt'] = $salt_str;

        $res = AdminModel::edit($data, $this->adminId);

        return $res ? $this->successReturn(['gen_pwd' => $gen_pwd]) : $this->errorReturn();
    }


}
